//
//  IOTCContact.m
//  Contacts
//
//  Created by User-16 on 3/14/14.
//  Copyright (c) 2014 IOTrainingCamp. All rights reserved.
//

#import "IOTCTask.h"

@interface IOTCTask ()

@property(retain, readwrite) NSString* description;
@property (readwrite) bool isDone;
@property (retain) UILocalNotification* notification;

@end

@implementation IOTCTask

- (id)initWithTaskDescription:(NSString*) description
{
    self = [super init];
    if (self) {
        self.description = description;
        self.isDone = NO;
    }
    return self;
}


-(void) finish
{
    self.isDone = YES;
}

-(void) resetUnfinished
{
    self.isDone = NO;
}

-(NSString*) startAndEndTimeDescription
{
    NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
    [dateFormatters setDateFormat:@"dd-MMM-yyyy hh:mm"];
    [dateFormatters setDateStyle:NSDateFormatterShortStyle];
    [dateFormatters setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatters setDoesRelativeDateFormatting:YES];
    [dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];
    NSString* startTime = self.startDate? [dateFormatters stringFromDate:self.startDate] : @"NA";
    NSString* endTime = self.endDate?[dateFormatters stringFromDate:self.endDate] : @"NA";
    
    NSString* timedescription = [NSString stringWithFormat:@"Start: %@ End: %@ ", startTime, endTime];
    
  
    return timedescription;
}

-(void) setLocalNotification
{
    if(!self.endDate) return;
    
    UILocalNotification* taskEndDateNotification = [[UILocalNotification alloc] init];
    taskEndDateNotification.fireDate = self.endDate;
    taskEndDateNotification.alertBody = self.description;
    taskEndDateNotification.alertAction = @"Task Due Date!";
    taskEndDateNotification.timeZone = [NSTimeZone systemTimeZone];
    taskEndDateNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
    
    [[UIApplication sharedApplication] scheduleLocalNotification:taskEndDateNotification];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];
    
    self.notification = taskEndDateNotification;

}

-(void) cancelNotification
{
    if(self.notification)
    {
        [[UIApplication sharedApplication] cancelLocalNotification: self.notification];
    }
}

   

@end
